home *** CD-ROM | disk | FTP | other *** search
- /****************************************************************************/
- /* */
- /* Application: Color Picker Test */
- /* */
- /* Description: This application demonstrates how to use the color */
- /* picker package to dynamically change colors in a */
- /* custom palette. The program basically displays 16 */
- /* squares, each representing a separate entry in the */
- /* palette. Clicking the mouse in any box allows the */
- /* user to redefine that box's color with the color */
- /* picker package routines. The trap ActivatePalette */
- /* is called after each color change to guarantee the */
- /* window is updated with the palette changes. Without */
- /* this safeguard, random results may occur and the */
- /* color change may not take affect until the window */
- /* has physically changed or moved. Finally, the first */
- /* and last entries in the palette cannot be changed */
- /* and are always defined to black and white. This is */
- /* done because ActivatePalette will only update 14 */
- /* non-b/w Tolerant colors. The remaining 2 colors */
- /* if defined as b/w will be updated, otherwise QD will */
- /* return the color in the palette which is the closest */
- /* match to the RGB values of these entries. */
- /* */
- /* File: Color Picker.c */
- /* */
- /* Programmer: Edgar Lee */
- /* Organization: Apple Computer, Inc. */
- /* Department: Developer Technical Support, DTS */
- /* Language: C (Think C version 4.0.4) */
- /* Date Created: 10-02-91 */
- /* */
- /****************************************************************************/
-
- #include <AppleEvents.h>
- #include <Errors.h>
- #include <Events.h>
- #include <Fonts.h>
- #include <GestaltEqu.h>
- #include <Memory.h>
- #include <Menus.h>
- #include <OSUtils.h>
- #include <QDOffscreen.h>
- #include <QuickDraw.h>
- #include <Resources.h>
- #include <Script.h>
- #include <ToolUtils.h>
- #include <Windows.h>
- #include <Palettes.h>
-
- /* Constant Declarations */
-
- #define TOTALCOLORS 16
-
- #define BOXSIZE 75
- #define WWIDTH ((TOTALCOLORS / 4) * BOXSIZE)
- #define WHEIGHT ((TOTALCOLORS / 4) * BOXSIZE )
-
- #define WLEFT (((screenBits.bounds.right - screenBits.bounds.left) - WWIDTH) / 2)
- #define WTOP (((screenBits.bounds.bottom - screenBits.bounds.top) - WHEIGHT) / 2)
-
- /* Global Variable Definitions */
-
- WindowPtr gWindow;
- PaletteHandle gPalette;
-
- void initMac();
- void createWindow();
- void createPalette();
- void drawImage();
-
- void doEventLoop();
- void doInContent();
-
- main()
- {
- initMac();
-
- createWindow();
- createPalette();
- drawImage();
-
- doEventLoop();
-
- DisposeWindow( gWindow );
- }
-
- void initMac()
- {
- MaxApplZone();
-
- InitGraf( &thePort );
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs( nil );
- InitCursor();
- FlushEvents( 0, everyEvent );
- }
-
- void createWindow()
- {
- Rect wBounds;
-
- /* Create a window to display the image. */
-
- SetRect( &wBounds, WLEFT, WTOP, WLEFT + WWIDTH, WTOP + WHEIGHT );
-
- gWindow = NewCWindow( 0L, &wBounds, "\pColor Picker Test", true, documentProc,
- (WindowPtr)-1L, true, 0L );
-
- ShowWindow( gWindow );
- SetPort( gWindow );
- }
-
- void createPalette()
- {
- int index;
- RGBColor aColor;
-
- /* Create the palette of size TOTALCOLORS. */
-
- gPalette = NewPalette( TOTALCOLORS, nil, pmTolerant, 0 );
-
- /* Assign a color to the first 15 palette entries. */
-
- for (index = 0; index < (TOTALCOLORS - 1); index++)
- {
- aColor.blue = index * (0xffff / (TOTALCOLORS - 2));
- aColor.red = aColor.green = 0;
- SetEntryColor( gPalette, index, &aColor );
- }
-
- /* Set the last entry to white. */
-
- aColor.red = aColor.green = aColor.blue = 0xffff;
- SetEntryColor( gPalette, (TOTALCOLORS - 1), &aColor );
-
- /* Attach the new palette to the main window. */
-
- SetPalette( gWindow, gPalette, true );
- }
-
- void drawImage()
- {
- int i;
- int x, y;
- Rect rect;
- RGBColor aColor;
-
- /* Draw a grid of colors to represent each color entry in the palette. */
-
- for (i = 0; i < TOTALCOLORS; i++)
- {
- x = (i % 4) * BOXSIZE;
- y = (i / 4) * BOXSIZE;
-
- GetEntryColor( gPalette, i, &aColor );
- RGBForeColor( &aColor );
-
- SetRect( &rect, x, y, x + BOXSIZE, y + BOXSIZE );
- PaintRect( &rect );
- }
- }
-
- void doInContent( thePoint )
- Point thePoint;
- {
- int paletteIndex = -1;
- Point where;
- RGBColor currentColor, newColor;
- Rect rect;
- int ret;
-
- /* Get the palette entry index for the box drawn at the mouse click. */
-
- paletteIndex = ((thePoint.v / BOXSIZE) * 4) + (thePoint.h / BOXSIZE);
-
- /* If the paletteIndex isn't the first or last entry, do the following. */
-
- if (paletteIndex > 0 && paletteIndex < (TOTALCOLORS - 1))
- {
- /* Invert the selected box then beep the Mac. */
-
- SetRect( &rect, (paletteIndex % 4) * BOXSIZE,
- (paletteIndex / 4) * BOXSIZE,
- ((paletteIndex % 4) * BOXSIZE) + BOXSIZE,
- ((paletteIndex / 4) * BOXSIZE) + BOXSIZE );
- InvertRect( &rect );
- SysBeep( 1 );
-
- /* Get the RGB values for the color stored at this palette index. */
-
- GetEntryColor( gPalette, paletteIndex, ¤tColor );
-
- /* Open the color picker dialog to select new RGB values. */
-
- where.h = where.v = -1;
-
- if (GetColor( where, "\pSelect a new palette color.", ¤tColor, &newColor ))
- {
- /* Assign the new RGB values to this entry. */
-
- SetEntryColor( gPalette, paletteIndex, &newColor );
-
- /* Update the palette with the new colors. */
-
- ActivatePalette( gWindow );
-
- /* Redraw the image with the new palette colors. */
-
- drawImage();
- }
- else
- {
- /* Invert the rect back to its original state on a Cancel. */
-
- InvertRect( &rect );
- }
- }
- }
-
- void doEventLoop()
- {
- EventRecord anEvent;
- WindowPtr evtWind;
- short clickArea;
- Rect screenRect;
- Point thePoint;
-
- for (;;)
- {
- if (WaitNextEvent( everyEvent, &anEvent, 0, nil ))
- {
- if (anEvent.what == mouseDown)
- {
- clickArea = FindWindow( anEvent.where, &evtWind );
-
- if (clickArea == inDrag)
- {
- screenRect = (**GetGrayRgn ()).rgnBBox;
- DragWindow( evtWind, anEvent.where, &screenRect );
- }
- else if (clickArea == inContent)
- {
- if (evtWind != FrontWindow())
- SelectWindow( evtWind );
- else
- {
- thePoint = anEvent.where;
- GlobalToLocal( &thePoint );
- doInContent( thePoint );
- }
- }
- else if (clickArea == inGoAway)
- if (TrackGoAway( evtWind, anEvent.where ))
- return;
- }
- else if (anEvent.what == updateEvt)
- {
- evtWind = (WindowPtr)anEvent.message;
- SetPort( evtWind );
-
- BeginUpdate( evtWind );
- drawImage();
- EndUpdate (evtWind);
- }
- }
- }
- }